package com.cupster.func_db_litepal.crud;

import com.cupster.func_db_litepal.entity.User;
import org.litepal.LitePal;
import java.util.List;

public class UserHelper {

    public static boolean insert(String name ,String account){
        User user1 = new User();
        user1.setAccount(account);
        user1.setName(name);
        return  user1.save();
    }

    public static boolean update(String name){
        User user = LitePal.find(User.class , 1 );
        if (user == null ){
            return false;
        }else {
            user.setName(name);
            return user.save();
        }
    }
    public static boolean update(long id ,String name){
        User user = new User();
        user.setName(name);
        return user.update(id)>0;
    }

    public static boolean delete(long id){
        return LitePal.delete(User.class ,id)>0;
    }

    public static boolean deleteAll(){
        return LitePal.deleteAll(User.class)>0;
    }

    public static User queryById(long id){
        return LitePal.find(User.class ,id);
    }

    public static String queryAll(){
        List<User> users = LitePal.findAll(User.class);
        StringBuilder stringBuilder = new StringBuilder();
        for (User u : users){
            stringBuilder.append("\n");
            stringBuilder.append("id:");
            stringBuilder.append(u.getId());
            stringBuilder.append("\n");
            stringBuilder.append("name:");
            stringBuilder.append(u.getName());
            stringBuilder.append("\n");
            stringBuilder.append("account:");
            stringBuilder.append(u.getAccount());
        }
        return stringBuilder.toString();
    }

    public static List<User> queryWhere(String condition ){
        return LitePal.where(condition).order("id").find(User.class);
    }
}
